package interlocking.ui;

import interlocking.controller.InterlockingController;
import interlocking.model.*;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import javax.swing.Timer;

public class StationPanel extends JPanel{
    private static final Color TRACK_COLOR = new Color(139, 69, 19); // 棕色轨道
    private static final Color SWITCH_COLOR = Color.BLACK;
    private static final Color SIGNAL_COLOR = Color.BLUE;
    private static final Color TRAIN_COLOR = Color.ORANGE;
    private final InterlockingController controller;
    private Timer animationTimer;



    public StationPanel(InterlockingController controller) {
        this.controller = controller;
        setBackground(Color.WHITE);


//        animationTimer=new Timer(50,e ->{      //计时器
//
//            //在这里添加条件判断，如果信号灯P1是绿色的，则可以运行；反正停止
//            Map<String,Signal> signalMap=controller.getSignals();
//            Signal signalP1=signalMap.get("P1");
//            if(signalP1.getAspect()== Signal.SignalAspect.PROCEED){
//                if(signalMap.get("S1").getAspect()== Signal.SignalAspect.PROCEED&&signalMap.get("S2").getAspect()== Signal.SignalAspect.PROCEED){
//                    //首先完成了对信号灯的判断，接下来要完成对道岔的判断
//                    Route route=controller.getRoutes().get("Route1");
//                    if(controller.establishRoute(route)){
//                        controller.updateTrains();
//                        repaint();
//                    }else {
//                        repaint();
//                    }
////                    controller.updateTrains();
////                    repaint();
//                }
//
//                if(signalMap.get("S3").getAspect()== Signal.SignalAspect.PROCEED&&signalMap.get("S4").getAspect()== Signal.SignalAspect.PROCEED){
//                    Route route=controller.getRoutes().get("Route2");
//                    if(controller.establishRoute(route)){
//                        controller.updateTrains2();
//                        repaint();
//                    }else {
//                        repaint();
//                    }
//                    //controller.updateTrains2();
//
//                }
//
//                if(signalMap.get("S5").getAspect()== Signal.SignalAspect.PROCEED&&signalMap.get("S6").getAspect()== Signal.SignalAspect.PROCEED){
//                    Route route=controller.getRoutes().get("Route3");
//                    if(controller.establishRoute(route)){
//                        controller.updateTrains3();
//                        repaint();
//                    }else {
//                        repaint();
//                    }
//                    //controller.updateTrains3();
//
//                }
//                //controller.updateTrains();
//                repaint();
//            }
//            repaint();
//
//        });
//        animationTimer.start();


    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 绘制站场图
        drawStationLayout(g);

        // 绘制设备状态
        drawDevicesStatus(g);

        //绘制列车
        Map<String,Train> trainMap=controller.getTrainsController();
        for(Train train:trainMap.values()){
            drawTrain(g,train);
        }

    }

    private void drawStationLayout(Graphics g) {
        // 这里应该根据实际的站场图数据绘制
        g.setColor(TRACK_COLOR);
        g.drawLine(100, 300, 700, 300); // 轨道
        g.drawLine(200,300,250,250);   //1号岔路
        g.drawString("1",200,310);
        g.drawLine(250,250,450,250);
        g.drawLine(450,250,500,300);
        g.drawString("2",250,295);
        g.drawLine(250,300,300,350);
        g.drawString("5",300,360);
        g.drawLine(200,350,500,350);
        g.drawLine(500,350,550,300);
        g.drawString("4",550,310);
        g.drawString("3",500,300);


        //g.drawLine(200, 100, 200, 200); // 道岔分支
    }

    private void drawDevicesStatus(Graphics g) {
        // 绘制信号机状态
        Map<String, Signal> signals = controller.getSignals();
        for (Signal signal : signals.values()) {
            drawSignal(g, signal);
        }

        // 绘制道岔状态
        Map<String, Switch> switches = controller.getSwitches();
        for (Switch sw : switches.values()) {
            drawSwitch(g, sw);
        }

        // 绘制轨道区段状态
        Map<String, TrackCircuit> trackCircuits = controller.getTrackCircuits();
        for (TrackCircuit tc : trackCircuits.values()) {
            drawTrackCircuit(g, tc);
        }
    }

    private void drawSignal(Graphics g, Signal signal) {
        // 根据信号机位置和状态绘制
        int x = signal.getPosition_x();
        int y = signal.getPosition_y();

        // 绘制信号机基础
        g.setColor(SIGNAL_COLOR);
        g.fillRect(x, y, 10, 20);

        // 绘制信号灯
        switch (signal.getAspect()) {
            case STOP:
                g.setColor(Color.RED);
                break;
            case PROCEED:
                g.setColor(Color.GREEN);
                break;
            case CAUTION:
                g.setColor(Color.YELLOW);
                break;
        }
        g.fillOval(x, y, 10, 10);
    }


    // 辅助方法：绘制粗线
    private void drawThickLine(Graphics g, int x1, int y1, int x2, int y2, int thickness) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(thickness));
        g2d.drawLine(x1, y1, x2, y2);
        g2d.setStroke(new BasicStroke(1)); // 恢复默认
    }

    // 辅助方法：绘制虚线
    private void drawDashedLine(Graphics g, int x1, int y1, int x2, int y2, int thickness) {
        Graphics2D g2d = (Graphics2D) g;
        float[] dash = {5.0f};
        g2d.setStroke(new BasicStroke(thickness, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
        g2d.drawLine(x1, y1, x2, y2);
        g2d.setStroke(new BasicStroke(1));
    }


    private void drawSwitch(Graphics g, Switch sw) {
        // 根据道岔位置和状态绘制
        int x = sw.getPosition_x();
        int y = sw.getPosition_y();

        int switchLength=20;
        int branchLength=25;
        int trackWidth=4;


        g.setColor(SWITCH_COLOR);

       if("SW1".equals(sw.getId())){
           if(sw.getPosition()== Switch.SwitchPosition.NORMAL){
               //g.drawLine(x,y,x+20,y);
               //直通轨道
               g.fillRect(x, y - trackWidth/2, switchLength, 4);
               // 岔线（未接通）
               drawDashedLine(g, x, y, x + branchLength, y - branchLength, trackWidth);
           }else {
               g.fillRect(x, y - trackWidth/2, branchLength, 4); // 短直段
               drawThickLine(g, x, y, x + branchLength, y - branchLength, trackWidth);
               //g.drawLine(x,y,x+20,y-20);
           }
       }

        if("SW2".equals(sw.getId())){
            if(sw.getPosition()== Switch.SwitchPosition.NORMAL){
                //g.drawLine(x,y,x+20,y);
                g.fillRect(x, y - trackWidth/2, switchLength, 4);
                drawDashedLine(g, x, y, x + branchLength, y + branchLength, trackWidth);

            }else {
                g.fillRect(x, y - trackWidth/2, branchLength, 4);
                drawThickLine(g, x, y, x + branchLength, y + branchLength, trackWidth);
                //g.drawLine(x,y,x+20,y+20);
            }
        }

        if("SW3".equals(sw.getId())){
            if(sw.getPosition()== Switch.SwitchPosition.NORMAL){
                g.fillRect(x - switchLength, y - trackWidth/2, switchLength, 4);
                drawDashedLine(g, x, y, x - branchLength, y - branchLength, trackWidth);
                //g.drawLine(x,y,x-20,y);
            }else {
                g.fillRect(x - branchLength, y - trackWidth/2, branchLength, 4);
                drawThickLine(g, x, y, x - branchLength, y - branchLength, trackWidth);
                //g.drawLine(x,y,x-20,y-20);
            }
        }

        if("SW4".equals(sw.getId())){
            if(sw.getPosition()== Switch.SwitchPosition.NORMAL){
                g.fillRect(x - switchLength, y - trackWidth/2, switchLength, 4);
                drawDashedLine(g, x, y, x - branchLength, y + branchLength, trackWidth);
                //g.drawLine(x,y,x-20,y);
            }else {
                g.fillRect(x - branchLength, y - trackWidth/2, branchLength, 4);
                drawThickLine(g, x, y, x - branchLength, y + branchLength, trackWidth);
                //g.drawLine(x,y,x-20,y+20);
            }
        }

        // 显示锁定状态
        if (sw.isLocked()) {
            g.setColor(Color.BLUE);
            g.drawString("L", x + 10, y - 5);
        }

        if (sw.isBlocked()) {
            g.setColor(Color.RED);
            g.drawString("B", x + 20, y - 5);
        }
    }

    private void drawTrackCircuit(Graphics g, TrackCircuit tc) {
        // 根据轨道区段位置和状态绘制
        int x = tc.getPosition_x(); // 示例位置
        int y = tc.getPosition_y();

        if (tc.isOccupied()) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.GREEN);
        }
        g.drawString(tc.getId(), x, y);
    }

    private void drawTrain(Graphics g, Train train){
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(TRAIN_COLOR);
        g2d.setStroke(new BasicStroke(5));
        g2d.drawLine(train.getX(),train.getY(),train.getX()+train.getLength(),train.getY());
    }

}
